﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProBugTracker.Data.Interface;

namespace ProBugTracker.Data
{
    public class PermissionManagerData : IPermissionManagerData
    {
        public bool AddNewPermission(RolePermissionDB userPermission)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    proBugTrackerEntities.AddToRolePermissionDBs(userPermission);
                    proBugTrackerEntities.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public RoleAssignPermissionDB AddRolePermission(RoleAssignPermissionDB rolePermission)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    proBugTrackerEntities.AddToRoleAssignPermissionDBs(rolePermission);
                    proBugTrackerEntities.SaveChanges();
                    return rolePermission;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool RemoveRolePermission(int roleID, int permissionID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    RoleAssignPermissionDB roleAssignPermission = (from r in proBugTrackerEntities.RoleAssignPermissionDBs where r.UserRoleID == roleID && r.UserPermissionID == permissionID select r).FirstOrDefault();
                    roleAssignPermission.Active = false;
                    proBugTrackerEntities.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public IList<RoleAssignPermissionDB> ReadRolePermissionByRoleID(int roleID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    return  (from r in proBugTrackerEntities.RoleAssignPermissionDBs where r.UserRoleID == roleID select r).ToList();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        public RolePermissionDB ReadPermissionByPermissionID(int permissionID)
        {
            try
            {
                using (ProBugTrackerEntities proBugTrackerEntities = new ProBugTrackerEntities())
                {
                    return (from r in proBugTrackerEntities.RolePermissionDBs where r.ID == permissionID select r).SingleOrDefault();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
